//
//  NewChatAudioCell.m
//  学生汇
//
//  Created by yull on 14/12/28.
//  Copyright (c) 2014年 于博. All rights reserved.
//

#import "ChatAudioCell.h"
#import "UIColor+MethodExt.h"

@implementation ChatAudioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentImageView.userInteractionEnabled = YES;
        
        self.audioImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        self.audioImageView.userInteractionEnabled = YES;
        self.audioImageView.animationDuration = 1.0f;
        self.audioImageView.backgroundColor = [UIColor clearColor];
        [self.chatContentView addSubview:self.audioImageView];
        
        self.leftImageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"chat_audio_left_3"],[UIImage imageNamed:@"chat_audio_left_2"],[UIImage imageNamed:@"chat_audio_left_1"],[UIImage imageNamed:@"chat_audio_left_2"], nil];
        self.rightImageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"chat_audio_right_3"],[UIImage imageNamed:@"chat_audio_right_2"],[UIImage imageNamed:@"chat_audio_right_1"],[UIImage imageNamed:@"chat_audio_right_2"], nil];
        
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:self.audioImageView.frame];
        self.indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        self.indicatorView.hidesWhenStopped = YES;
        [self.chatContentView addSubview:self.indicatorView];
        
        self.timeLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 12)];
        self.timeLengthLabel.backgroundColor = [UIColor clearColor];
        self.timeLengthLabel.font = [UIFont systemFontOfSize:14.0f];
        self.timeLengthLabel.centerY = self.contentView.height/2;
        self.timeLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        self.timeLengthLabel.textColor = [UIColor colorWith8BitRed:99 green:99 blue:99];
        //[self.contentView addSubview:self.timeLengthLabel];
        
        self.unreadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        self.unreadView.backgroundColor = [UIColor redColor];
        self.unreadView.layer.cornerRadius =  2.5f;
        self.unreadView.centerY = self.contentView.height/2;
        self.unreadView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        self.unreadView.hidden = YES;
        [self.contentView addSubview:self.unreadView];
        
        self.xSpace = 2;
        self.ySpace = 3;
    }
    return self;
}

+ (CGFloat)calculateCellHeight
{
    return 32 + 20 + 20;
}

- (void)drawCellWithChatMsg:(ChatBaseMsg *)chatMsg
{
    CGSize contentSize = CGSizeMake([self getAudioBackWidthWithAudioLength:chatMsg.msgLength], 20);
    self.chatContentView.size = contentSize;
    
    self.timeLengthLabel.text = [NSString stringWithFormat:@"%d\"",chatMsg.audioRestTime];
    self.timeLengthLabel.width = [self.timeLengthLabel sizeThatFits:CGSizeZero].width+1;
    
    [super drawCellWithChatMsg:chatMsg];
    
    if (chatMsg.roleType == ChatMsgRoleTypeReceive) {
        self.audioImageView.left = 5;
        
        self.audioImageView.animationImages = self.leftImageArray;
        self.audioImageView.image = [self.leftImageArray firstObject];
        
        self.timeLengthLabel.left = self.contentImageView.right + 5;
        
        self.failImageView.left = self.timeLengthLabel.right + 5;
//        self.sendingImageView.left = self.timeLengthLabel.right + 5;
        //self.unreadView.left = self.timeLengthLabel.right + 5;
        self.unreadView.left = self.timeLengthLabel.left;
    }else {
        self.audioImageView.left = self.chatContentView.width - 5 - self.audioImageView.width;
        
        self.audioImageView.animationImages = self.rightImageArray;
        self.audioImageView.image = [self.rightImageArray firstObject];
        
        self.timeLengthLabel.right = self.contentImageView.left - 5;
        
        self.failImageView.right = self.timeLengthLabel.left - 5;
//        self.sendingImageView.right = self.timeLengthLabel.left - 5;
        //self.unreadView.left = self.timeLengthLabel.right - 5;
    }
    
    [self updateWithAudioStatus:chatMsg.audioStatus];
    
    [self updateWithRead:chatMsg.isRead];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    
    id value = [change objectForKey:@"new"];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([keyPath isEqualToString:@"audioStatus"]) {
            [self updateWithAudioStatus:[value integerValue]];
        }else if ([keyPath isEqualToString:@"isRead"]) {
            [self updateWithRead:[value boolValue]];
        }else if ([keyPath isEqualToString:@"audioRestTime"]) {
            NSInteger playTime = [value integerValue];
            if (playTime > 0) {
                self.timeLengthLabel.text = [NSString stringWithFormat:@"%d\"",playTime];
                self.timeLengthLabel.width = [self.timeLengthLabel sizeThatFits:CGSizeZero].width;
            }
        }
    });
}

- (void)updateWithAudioStatus:(ChatMsgAudioStatus)status
{
    if (status == ChatMsgAudioStatusNone) {
        self.audioImageView.hidden = NO;
        [self.audioImageView stopAnimating];
        [self.indicatorView stopAnimating];
    }else if (status == ChatMsgAudioStatusLoading) {
        self.audioImageView.hidden = YES;
        [self.audioImageView stopAnimating];
        [self.indicatorView startAnimating];
    }else if (status == ChatMsgAudioStatusPlaying){
        self.audioImageView.hidden = NO;
        [self.audioImageView startAnimating];
        [self.indicatorView stopAnimating];
    }
}

- (void)updateWithRead:(BOOL)isRead
{
    if (self.chatMsg.roleType == ChatMsgRoleTypeReceive && !isRead) {
        self.unreadView.hidden = NO;
    }else {
        self.unreadView.hidden = YES;
    }
    
}

- (void)onTapGesture:(UITapGestureRecognizer *)tapGesture
{
    if (self.chatMsg.sendStatus == ChatMsgSendStatusFail) {
        [super onTapGesture:tapGesture];
    }else {
        if ([self.delegate respondsToSelector:@selector(onAudioCellTapWithChatMsg:)]) {
            [self.delegate onAudioCellTapWithChatMsg:self.chatMsg];
        }
    }
}
#pragma mark - Private
//计算长度
- (CGFloat)getAudioBackWidthWithAudioLength:(NSInteger)sec
{
    return 80.0f;
    
    /*
    CGFloat size = 200.0f / 6;
    CGFloat width = size;
    if (sec <= 0) {
        width = -1;
    } else if (sec <= 2) {
        width = size;
    } else if (sec <= 8) {
        width = (int) (size + ((float) ((sec - 2) / 6.0)) * size);
    } else if (sec <= 60) {
        width = (int) (2 * size + ((float) ((sec - 8) / 52.0)) * size);
    } else {
        width = -1;
    }
    return width + 40.0f;
     */
}

@end
